package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.Topic;
import edu.hcmus.sow.domain.TopicComment;

/**
 * DAO to manage TopicComment entities.
 * 
 */
@Repository("TopicCommentDAO")
@Transactional
public class TopicCommentDAOImpl extends AbstractHibernateDAO<TopicComment, Integer> implements TopicCommentDAO {
	public List<TopicComment> getCommentsByTopic(Topic topic)throws DataAccessException{
		String queryString = "FROM TopicComment tc join fetch tc.user tu WHERE tc.topic = ?";
		List<TopicComment> list = executeQuery(queryString, topic);
		return list;
	}
	
	public int getNumberCommentOfTopic(Topic topic) throws DataAccessException{
		String queryString = "select count(*) from TopicComment tc WHERE tc.topic = ?";
		Query query = createQuery(queryString, topic);
		long count = (Long) query.uniqueResult();
		return (int) count;
	}
	
	public int getTopicByTopicCommentID(Integer commentID) throws DataAccessException{
		String queryString = "select tc.topic.id from TopicComment tc WHERE tc.id = ?";
		Query query = createQuery(queryString, commentID);
		return  (Integer)query.uniqueResult();
	}
}
